package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.ApartmentLabelMapper;
import com.atguigu.lease.web.app.mapper.GraphInfoMapper;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public List<AppointmentItemVo> listItem() {
        List<AppointmentItemVo> list = viewAppointmentMapper.listItem();
        return list;
    }

    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        AppointmentDetailVo detailVo = new AppointmentDetailVo();

        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);
        BeanUtils.copyProperties(viewAppointment,detailVo);
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, viewAppointment.getApartmentId())
                .eq(ApartmentInfo::getIsDeleted, 0));
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        List<LabelInfo> apalabelInfos = apartmentLabelMapper.selectOneByApartmentId(apartmentInfo.getId());
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, apartmentInfo.getId()).eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getIsDeleted, 0));
        apartmentItemVo.setGraphVoList(graphInfos);
        apartmentItemVo.setLabelInfoList(apalabelInfos);
        apartmentItemVo.setMinRent(roomInfoService.minRentByApartmentId(apartmentInfo.getId()));
        detailVo.setApartmentItemVo(apartmentItemVo);

        return detailVo;
    }
}




